﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLySach
{
    public partial class frmQLSach : Form
    {
        public frmQLSach()
        {
            InitializeComponent();
        }
        DataTable db = new DataTable();
        string strMaSach;
        String strTenSach;
        String strMaTheLoai;
        String strTacGia;
        String strNhaXuatBan;
        String strSoLuong;
        private void btnDanhSach_Click(object sender, EventArgs e)
        {

            
        }

        private void dgvQLSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                 strMaSach = this.dgvQLSach.Rows[e.RowIndex].Cells[0].Value.ToString();
                 strTenSach = this.dgvQLSach.Rows[e.RowIndex].Cells[1].Value.ToString();
                 strMaTheLoai = this.dgvQLSach.Rows[e.RowIndex].Cells[2].Value.ToString();
                 strTacGia = this.dgvQLSach.Rows[e.RowIndex].Cells[3].Value.ToString();
                 strNhaXuatBan = this.dgvQLSach.Rows[e.RowIndex].Cells[4].Value.ToString();
                 strSoLuong = this.dgvQLSach.Rows[e.RowIndex].Cells[5].Value.ToString();
                /*DataGridView dgv = (DataGridView)sender;
                //Lay dong dc chon tren datagirview 
                //DataGridViewRow dr = dgv.SelectedRows[0];
                //dua du lieu vao cac control
                //txtMaSach.Text = dr.Cells[1].Value.ToString();
                //txtTacGia.Text  = dr.Cells[3].Value.ToString();
                //dòng muốn hiển thị
                //for (int i = 0; i < LoadData().Count; i++)
                //{
                //    comboBox1.Items.Add(LoadData()[i]);
                //}
                comboBox1.DisplayMember = "name";*/
                
                try 
	            {
                    TheLoaiBUS theloaiBUS = new TheLoaiBUS();
                    string strTenTheLoai = theloaiBUS.LayTenTheLoaiBUS(strMaTheLoai);
                    cbbTheLoai.Text = strTenTheLoai;
	            }
	            catch (Exception ex)
	            {

                    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
	            }
                //try
                //{
                //    SachBUS sachBUS = new SachBUS();
                //    List<SachDTO> ListSach = sachBUS.LayDachSachSachBUS();
                //    for (int i = 0; i < ListSach.Count; i++)
                //    {
                //        cbbMaSach.Items.Add(ListSach[i]);
                //        cbbMaSach.DisplayMember = "MaSach";
                //    }
                    
                //}
                //catch (Exception ex)
                //{

                //    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
                //}
                cbbMaSach.Text = strMaSach;
                txtSach.Text = strTenSach;
                txtTacGia.Text = strTacGia;
                txtNhaXuatBan.Text = strNhaXuatBan;
                txtSoLuong.Text=strSoLuong;
            }
            catch (Exception ex)
            {

                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            
        }

        private void frmQLSach_Load(object sender, EventArgs e)
        {
            btnSua.Text = "Sưa";
            btnThemSach.Enabled = true;
            btnXoa.Enabled = true;
            try
            {
                SachBUS sachBUS = new SachBUS();
                List<SachDTO> ListSach = sachBUS.LayDachSachSachBUS();
                dgvQLSach.DataSource = ListSach;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            //try
            //{
            //    TheLoaiBUS theloaiBUS = new TheLoaiBUS();
            //    db = theloaiBUS.GetDataTable();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //}
            btnSua.Enabled = true;
            cbbMaSach.Text = "Chọn mã Sách";
            cbbTheLoai.Text="Chọn Thể Loại";
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
                //txtMaSach.Enabled = true;
                txtNhaXuatBan.Enabled = true;
                txtSach.Enabled = true;
                txtSoLuong.Enabled = true;
                txtTacGia.Enabled = true;
                //txtTheLoai.Enabled = true;
            if (btnSua.Text=="Sưa")
            {
                btnSua.Text = "Lưu";
                SachBUS sachBUS = new SachBUS();
                List<SachDTO> ListSach = sachBUS.LayDachSachSachBUS();
            }
            else
            {
                try
                {
                    TheLoaiBUS theloaiBUS = new TheLoaiBUS();
                    List<TheLoaiDTO> ListTheLoai = theloaiBUS.LayDachSachTheLoaihBUS();
                    for (int i = 0; i < ListTheLoai.Count; i++)
                    {
                        cbbTheLoai.Items.Add(ListTheLoai[i]);
                        cbbTheLoai.DisplayMember = "TenTheLoai";
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
                strMaSach = cbbMaSach.Text;
                strTenSach = txtSach.Text;
                //string strTenTheLoai = cbbTheLoai.Text;
                //try
                //{
                //    TheLoaiBUS theloaiBUS = new TheLoaiBUS();
                //     strMaTheLoai = theloaiBUS.LayMaTheLoaiBUS(strTenTheLoai);
                //}
                //catch (Exception ex)
                //{

                //    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
                //}
                strTacGia = txtTacGia.Text;
                strNhaXuatBan = txtNhaXuatBan.Text;
                strSoLuong = txtSoLuong.Text;
                try
                {
                    SachBUS sachBUS = new SachBUS();
                    int i = sachBUS.CapNhatDanhSachDAL(strMaSach, strTenSach, strMaTheLoai, strTacGia, strNhaXuatBan, strSoLuong);
                    MessageBox.Show("cập nhật thành công" + i, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }
            try
            {
                SachBUS sachBUS = new SachBUS();
                List<SachDTO> ListSach = sachBUS.LayDachSachSachBUS();
                dgvQLSach.DataSource = ListSach;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }

        }

        private void btnThemSach_Click(object sender, EventArgs e)
        {
            strMaSach = cbbMaSach.Text;
            strTenSach = txtSach.Text;
            strMaTheLoai = cbbTheLoai.Text;
            strTacGia = txtTacGia.Text;
            strNhaXuatBan = txtNhaXuatBan.Text;
            strSoLuong = txtSoLuong.Text;
            string strTenTheLoai = cbbTheLoai.Text;
            try
            {
                TheLoaiBUS theloaiBUS = new TheLoaiBUS();
                strMaTheLoai = theloaiBUS.LayMaTheLoaiBUS(strTenTheLoai);
            }
            catch (Exception ex)
            {

                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            try
            {
                SachBUS sachBUS = new SachBUS();
                int i = sachBUS.ThemDanhSachDAL(strMaSach,strTenSach,strMaTheLoai,strTacGia,strNhaXuatBan,strSoLuong);
            }
            catch (Exception ex)
            {

                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            try
            {
                SachBUS sachBUS = new SachBUS();
                List<SachDTO> ListSach = sachBUS.LayDachSachSachBUS();
                dgvQLSach.DataSource = ListSach;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi" + ex, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            DataTable dt;
            //try
            //{
            //    SachBUS sachBUS = new SachBUS();
            //    dt= sachBUS.GetDataTableBUS();
            //}
            //catch (Exception)
            //{
                
            //    throw;
            //}
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //add code xoa
        }

        private void frmQLSach_FormClosing(object sender, FormClosingEventArgs e)
        {
            //DialogResult drDong= MessageBox.Show("Có lưu thay đổi","Thông báo",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
            //if (drDong == DialogResult.Yes)
            //{
            //    //add code cap nhat
            //    e.Cancel = false;
            //}
            //else if (drDong == DialogResult.No)
            //{
            //    e.Cancel = false;
            //}
            //else
            //{
            //    e.Cancel = true;
            //}
        }
    }
}
